import os
import random,pathlib
import shutil


def GenRandFileName() -> set:
    fnameset = set()
    while True:
        if len(fnameset) == 50:
            break
        fname = bytes(random.choices(range(97,123),k=4)).decode('utf-8')
        fnameset.add(fname)
    return  fnameset


def InsertFileFromSet(fnameset:set,currtdst:str="."):
    p = pathlib.Path(currtdst)
    ddt = p / 'a' / 'b' / 'c' / 'd'
    ddt.mkdir(parents=True, exist_ok=True)
    while True:
        if len(fnameset) == 0:
            break
        ch = random.randint(1,4)
        if ch == 1:
            CreateNewFile = p / 'a' / fnameset.pop()
            CreateNewFile.touch()
        elif ch == 2:
            CreateNewFile = p / 'a' / 'b' / fnameset.pop()
            CreateNewFile.touch()
        elif ch == 3:
            CreateNewFile = p / 'a' / 'b' / 'c' / fnameset.pop()
            CreateNewFile.touch()
        else:
            CreateNewFile = p / 'a' / 'b' / 'c' / 'd' / fnameset.pop()
            CreateNewFile.touch()


# def MyIgnore(src,names):
#     ft = tuple(bytes([i for i in range(101,120)]).decode('utf-8'))
#     ig = filter(lambda x:x.startswith(ft),names)
#     return set(ig)


fnameset = GenRandFileName()
currtdst = "D://tmp"
#创建文件并随机插入到四个目录中
InsertFileFromSet(fnameset,currtdst)
# shutil.copytree("D://tmp","D://dst",ignore=MyIgnore)

p = pathlib.Path(currtdst)
np = list(p.rglob("[xyz]*"))

def MyCpFile(srclist:list,dst:str):
    s1 = [str(i) for i in srclist]
    s2 = [i.parts for i in srclist]
    p = pathlib.Path(dst) / 'a' / 'b' / 'c' / 'd'
    p.mkdir(parents=True,exist_ok=True)
    for i, filedir in zip(range(len(s1)),s2):
        if filedir[-2] == 'a':
            shutil.copy2(s1[i],dst+'/a')
        elif filedir[-2] == 'b':
            shutil.copy2(s1[i],dst+'/a'+'/b')
        elif filedir[-2] == 'c':
            shutil.copy2(s1[i],'{}/a/b/c'.format(dst))
        else:
            shutil.copy2(s1[i],'{}/a/b/c/d'.format(dst))
    print("原目录符合条件的文件: ",s1)
    print("复制完后目标目录: ",list(pathlib.Path(dst).rglob("[xyz]*")))

#题目最终实现
MyCpFile(np,'D://dst')
